#include <string.h>
#include <stdarg.h>
#include <Includes.h>
#include <main.h>
#include <DRV_Gpio.h>
#include <DRV_Usart.h>
//#include <DRV_Neul.h>
#include <DRV_Event.h>
#include <DRV_EC600S.h>
#include <DRV_EC600S_Sock.h>
#include <DRV_EC600S_CB.h>
#include <DRV_EC600S_Usim.h>
#include <DRV_EC600S_Event.h>
#include <AT_Main.h>
#include <OPP_Debug.h>
//#include "opple_main.h"
//#include "SVS_EventLog.h"
//#include "SVS_Mqtt.h"


static char ec600s_buf[NEUL_MAX_BUF_SIZE];
static char ec600s_wbuf[NEUL_MAX_BUF_SIZE];
static ST_EC600S_TX_DATA g_stEc600sTxData = {
    .sock=0,
	.haveData = 0,
	.tick = 0,
	.data = {0},
	.len = 0,
};

static neul_dev_operation_t ec600s_ops = {
    Ec600sDataRead,
    uart_data_write,
    NULL,
    NULL,
    NULL,
    NULL,
};

neul_dev_info_t ec600s_dev = {
	{0},
	{0},
	{0},
	{0},
	{0},
    ec600s_buf,
    ec600s_wbuf,
    0,
    NULL,
    &ec600s_ops,
    {0},
    {0}
};

T_MUTEX ec600sMux=NULL;
static EN_EC600S_DEV_ST g_enEc600sDevSt=EC600S_DEV_DIS;
static EN_EC600S_DEV_ST g_enEc600sDevLastSt=EC600S_DEV_DIS;
static EN_EC600S_RST_TYPE g_enEc600sRstType=EC600S_RST_NONE;
static U32 g_ulEc600sRstTo;
static EN_REG_STATE g_enEc600sRegSt = INVALID_REG;
static EN_REG_STATE g_enEc600sLastRegSt = INVALID_REG;
static U32 g_ulTPwrOff;
static U16 g_usEc600sDisConTimes = 0;      //30分钟内断网次数
static U16 g_usEc600sAllDisConTimes = 0;   //复位后总断网次数

/**
@brief 获取ec600s设备基本信息
@param 无
@return
**/
void Ec600sDevInfoGet(neul_dev_info_t *info)
{
    memcpy(info,&ec600s_dev,sizeof(neul_dev_info_t));
}

/**
@brief Ec600s初始化
@param 无
@return
**/
int Ec600sInit(void)
{
	/*int ret = 0;
    uart_config_t cfg ={
        .baud_rate = BAUD_DEF,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };

    printf("*******Ec600sInit uart init\r\n");
    ret = NbUartInit(NB_UART_NUM,&cfg,LOS_DEV_UART_DUALBUF_SIZE, 0);
    if (ret != 0)
    {
    	DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_ERROR, "init usart2 error\r\n");
        return -1;
    }*/
    printf("*******Ec600sInit uart init\r\n");
    usart_config_init(&usart1_context,BAUD_DEF,USART_WordLength_8b,USART_Parity_No);


    MUTEX_CREATE(g_stEc600sTxData.mutex);
    MUTEX_CREATE(ec600sMux);

    Ec600sUsimInit();
    Ec600sAtCmdInit();
    Ec600sSockInit();
    Ec600sTcpipInit();
    Ec600sRBInit();

    return 0;
}

/**
@brief 清空uart data缓存
@return 无
**/
int Ec600sUartDataFlush()
{
    //noted by wangtao
    //return uart_flush_input(NB_UART_NUM);
    return 0;
}

/**
@brief 设置ec600s设备状态
@param st 设备状态
@return 无
**/
void Ec600sStSet(EN_EC600S_DEV_ST st)
{
    if(st == EC600S_DEV_READY)
    {
        printf("Spend %d Seconds Attach Cellular Network Success!!!\r\n",OppTickCountGet()/1000);
    }
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_enEc600sDevSt=st;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 查询ec600s设备状态
@param 无
@return ec600s设备状态
**/
EN_EC600S_DEV_ST Ec600sStGet()
{
    EN_EC600S_DEV_ST st;

    if(ec600sMux==NULL)
        return g_enEc600sDevSt;
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    st=g_enEc600sDevSt;
    MUTEX_UNLOCK(ec600sMux);
    return st;
}

/**
@brief 设置上一次ec600s设备状态,记录状态变化前的状态
@param st 设备状态
@return 无
**/
void Ec600sLastStSet(EN_EC600S_DEV_ST st)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_enEc600sDevLastSt=st;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 查询ec600s上次设备状态,状态变化之前的状态
@param 无
@return ec600s设备状态
**/
EN_EC600S_DEV_ST Ec600sLastStGet()
{
    EN_EC600S_DEV_ST st;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    st=g_enEc600sDevLastSt;
    MUTEX_UNLOCK(ec600sMux);
    return st;
}

/**
@brief 设置ec600s注册状态
@param st 注册状态
@return 无
**/
void Ec600sRegStSet(EN_REG_STATE st)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_enEc600sRegSt=st;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 查询ec600s注册状态
@param 无
@return ec600s注册状态
**/
EN_REG_STATE Ec600sRegStGet()
{
    EN_REG_STATE reg;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    reg=g_enEc600sRegSt;
    MUTEX_UNLOCK(ec600sMux);
    return reg;
}

/**
@brief 设置ec600s最近一次的注册状态,本次变化前的状态
@param st 注册状态
@return 无
**/
void Ec600sLastRegStSet(EN_REG_STATE st)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_enEc600sLastRegSt=st;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 查询ec600s最近一次的注册状态,本次变化前的状态
@param 无
@return 注册状态
**/
EN_REG_STATE Ec600sLastRegStGet()
{
    EN_REG_STATE reg;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    reg=g_enEc600sLastRegSt;
    MUTEX_UNLOCK(ec600sMux);
    return reg;
}

/**
@brief 查询30分钟内ec600s网络断连次数
@param 无
@return 断连次数
**/
U32 Ec600sDisConTimesGet()
{
    U32 times;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    times = g_usEc600sDisConTimes;
    MUTEX_UNLOCK(ec600sMux);
    return times;
}
/**
@brief 设置30分钟内ec600s网络断连次数
@param times 断连次数
@return 无
**/
void Ec600sDisConTimesSet(U32 times)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_usEc600sDisConTimes = times;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 增加30分钟内ec600s网络断连次数
@param num 断连次数
@return 无
**/
void Ec600sDisConTimesAdd(U32 num)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_usEc600sDisConTimes += num;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 查询ec600s总网络断连次数
@param 无
@return 断连次数
**/
U32 Ec600sAllDisConTimesGet()
{
    U32 times;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    times = g_usEc600sAllDisConTimes;
    MUTEX_UNLOCK(ec600sMux);
    return times;
}

/**
@brief 增加ec600s总网络断连次数
@param 无
@return 断连次数
**/
void Ec600sAllDisConTimesAdd(U32 num)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_usEc600sAllDisConTimes += num;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 设置ec600s复位类型和复位保护时间
@param type 复位类型
@return 无
**/
void Ec600sRstTypeSet(EN_EC600S_RST_TYPE type)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_enEc600sRstType=type;
    //设置复位超时保护时间
    if(type==EC600S_RST_CMD)
        g_ulEc600sRstTo=EC600S_CMD_RESET_TO;
    else
        g_ulEc600sRstTo=EC600S_HARD_RESET_TO;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief 查询ec600s复位类型
@param 无
@return ec600s复位类型
**/
EN_EC600S_RST_TYPE Ec600sRstTypeGet()
{
    EN_EC600S_RST_TYPE type;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    type=g_enEc600sRstType;
    MUTEX_UNLOCK(ec600sMux);
    return type;
}

/**
@brief 设置ec600s复位超时时间
@param to 超时时间,单位毫秒
@return 无
**/
void Ec600sRstTimeoutSet(U32 to)
{
    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    g_ulEc600sRstTo=to;
    MUTEX_UNLOCK(ec600sMux);
}

/**
@brief ec600 kress power key
@param t gpio action delay time,单位毫秒
@return ec600s复位类型
**/
int Ec600sPwrKeyPullDown(int t)
{
    EC4G_PWRKEY(1);
    vTaskDelay(t);
    EC4G_PWRKEY(0);
    return 0;
}

//单位毫秒
int Ec600sRstKeyPullDown(int t)
{
    EC4G_RESET(1);
    vTaskDelay(t);
    EC4G_RESET(0);
    return 0;
}

/**
@brief 查询ec600s复位超时时间
@param 无
@return ec600s复位类型
**/
U32 Ec600sRstTimeoutGet()
{
    U32 to;

    MUTEX_LOCK(ec600sMux, MUTEX_WAIT_ALWAYS);
    to=g_ulEc600sRstTo;
    MUTEX_UNLOCK(ec600sMux);
    return to;
}

/**
@brief ec600s power off, fct低功耗调用
@param 无
@return
**/
int Ec600sPwrOff()
{
    Ec600sPwrKeyPullDown(PWRKEYOFF_TIME);
    return 0;
}

/**
@brief is ec600s power on?, fct复位调用
@param 无
@return
**/
int Ec600sIsPwrOn()
{
    if(Ec600sStGet()<EC600S_DEV_PWRON)
        return 0;
    return 1;
}

/**
@brief 增加ec600s总网络断连次数
@param 无
@return 断连次数
**/
void Ec600sSaveDisLog(int timeout)
{
    //noted by wangtao
	/*static U32 tick = 0;
	ELOG_QITEM_ST sQitem = {0};

	if(0 == tick)
    {
		tick = OppTickCountGet();
	}
    else if(OppTickCountGet() - tick > timeout)
	{
	    //有断连发生记录下
	    if(Ec600sDisConTimesGet()>0)
        {
            sQitem.u8Event = EL_EVENT_NB_DISCON_TIMES;
            sQitem.au8Argp[0] = (Ec600sDisConTimesGet()>>8)&0xFF;
            sQitem.au8Argp[1] = Ec600sDisConTimesGet()&0xFF;
            if(ApsEventLogItemPush(&sQitem)!=0)
            {
                DEBUG_LOG(DEBUG_MODULE_MAIN, DLL_ERROR, "Save event log fail!\r\n");
            }
            Ec600sDisConTimesSet(0);
        }
		tick = 0;
	}*/
}

void Ec600sPrintInfoDelay(int timeout)
{
	static U32 tick = 0;

	if(0 == tick)
    {
		tick = OppTickCountGet();
	}
    else if(OppTickCountGet() - tick > timeout)
    {
		DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_ERROR, "Ec600sThread run..........\r\n");
		tick = 0;
	}
}


int Ec600sReset(EN_EC600S_RST_TYPE type)
{
    int ret=0;

    DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "Ec600sReset type=%d\r\n", type);

    //清空uart rx fifo缓存
    Ec600sUartDataFlush();
    //没插SIM卡AT_QPOWD快速返回消息并设置EC600S_DEV_WAIT_PWROFF状态，设置EC600S_DEV_RESET状态需提前
    Ec600sStSet(EC600S_DEV_RESET);

    if(type==EC600S_RST_CMD)
    {
        ret = Ec600sAT_QPOWD();
    }
    else if(type==EC600S_RST_EVENT_CMD)
    {
        type=EC600S_RST_CMD;
        ret = EC600S_QPOWD_EVENT_DO(EVENT_WAIT_DEFAULT);
    }
    else if(type==EC600S_RST_PWRKEY)
    {
        ret = Ec600sPwrKeyPullDown(PWRKEYOFF_TIME);
    }
    else if(type==EC600S_RST_RSTKEY)
    {
        ret = Ec600sRstKeyPullDown(NRSTKEYOFF_TIME);
    }
    //复位命令下发成功
    //if(ret==0)
    {
        Ec600sRstTypeSet(type);
        //没插SIM卡AT_QPOWD快速返回消息并设置EC600S_DEV_WAIT_PWROFF状态，设置EC600S_DEV_RESET状态需提前
        //Ec600sStSet(EC600S_DEV_RESET);
        Ec600sCtxSet(CTX_INIT);
        //mqtt noted by wangtao
        //MqttSessionAllReset();
    }
    return ret;
}

int Ec600sRstLoop()
{
    static U32 tick=0;
    EN_EC600S_RST_TYPE rstType;

    rstType=Ec600sRstTypeGet();

    if(EC600S_RST_NONE==rstType)
    {
        tick=0;
        return 0;
    }

    if(tick==0)
        tick=OppTickCountGet();

    if(OppTickCountGet()-tick>Ec600sRstTimeoutGet())
    {
        if(EC600S_RST_CMD==rstType)
        {
            Ec600sReset(EC600S_RST_PWRKEY);
        }
        else if(EC600S_RST_PWRKEY==rstType)
        {
            Ec600sReset(EC600S_RST_RSTKEY);
        }
        else if(EC600S_RST_RSTKEY==rstType)
        {
            Ec600sReset(EC600S_RST_RSTKEY);
        }
        tick=0;
    }

    return 0;
}

/**
@brief 设置ec600s注册状态
@param sock 套接字
@param data_type 数据类型
@param data 数据
@param len 数据长度
@return 0成功，非0错误
**/
int Ec600sDataSend(int sock,EN_EC600S_DATA_TYPE data_type,U8 data[],int len)
{
	MUTEX_LOCK(g_stEc600sTxData.mutex,MUTEX_WAIT_ALWAYS);
    if(g_stEc600sTxData.haveData==1)
    {
		MUTEX_UNLOCK(g_stEc600sTxData.mutex);
        return 1;
    }
	//do not have data
	if(data_type==EN_DATA_TYPE_STR)
	{
    	if(len > EC600S_MAX_MTU)
        {
    		DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "len %d > STR MTU %d\r\n", len, EC600S_MAX_MTU);
    		MUTEX_UNLOCK(g_stEc600sTxData.mutex);
    		return 2;
    	}
    }
    else if(data_type==EN_DATA_TYPE_HEX)
    {
    	if(len > EC600S_HEX_MAX_MTU)
        {
    		DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "len %d > HEX MTU %d\r\n", len, EC600S_HEX_MAX_MTU);
    		MUTEX_UNLOCK(g_stEc600sTxData.mutex);
    		return 3;
    	}
    }
    else
    {
        MUTEX_UNLOCK(g_stEc600sTxData.mutex);
        return 4;
    }
	g_stEc600sTxData.haveData = 1;
    g_stEc600sTxData.sock = sock;
	g_stEc600sTxData.len = len;
	memcpy(g_stEc600sTxData.data,data,len);
    g_stEc600sTxData.data_type=data_type;
	MUTEX_UNLOCK(g_stEc600sTxData.mutex);

    Ec600sTcpAckTo(sock, 1);

	return 0;
}

static int TxDataProc()
{
	int ret;
    static U32 stick = 0;

	MUTEX_LOCK(g_stEc600sTxData.mutex,MUTEX_WAIT_ALWAYS);
	if(g_stEc600sTxData.haveData){
		if(0 == g_stEc600sTxData.tick){
			g_stEc600sTxData.tick = OppTickCountGet();
		}else if(OppTickCountGet() - g_stEc600sTxData.tick > EC600S_TXDATA_DEFAULT_TO){
			g_stEc600sTxData.haveData = 0;
			g_stEc600sTxData.tick = 0;
			MUTEX_UNLOCK(g_stEc600sTxData.mutex);
            stick = 0;
            //超时发送失败重启下模组
            Ec600sReset(EC600S_RST_CMD);
			return 0;
		}

        if(stick==0 || OppTickCountGet() - stick > EC600S_FAST_TXDATA_DEFAULT_TO){
            if(g_stEc600sTxData.data_type==EN_DATA_TYPE_STR)
            {
                ret = Ec600sAT_QISEND(g_stEc600sTxData.sock, g_stEc600sTxData.len);
                if(ret==0)
        		    ret = Ec600sAT_SendData(g_stEc600sTxData.data,g_stEc600sTxData.len);
            }
            else
            {
                ret = Ec600sAT_QISENDEX(g_stEc600sTxData.sock, g_stEc600sTxData.data,g_stEc600sTxData.len);
            }
            //-99.若连接已经建立，但是发送缓存已满
            if(-99 == ret)
            {
                stick = OppTickCountGet();
            }
            //-98.若连接未建立、 异常断开或者参数不正确
            else if(-98 == ret)
            {
                Ec600sSockStSet(g_stEc600sTxData.sock, SS_DISCONNECTING);
            }

    		//send succ or send packet bigger than mtu
    		if(0 == ret || -98 == ret){
    			g_stEc600sTxData.haveData = 0;
    			g_stEc600sTxData.tick = 0;
                g_stEc600sTxData.sock = 0;
                stick = 0;
    		}
            //g_stTxData.haveData = 0;
        }
	}
	MUTEX_UNLOCK(g_stEc600sTxData.mutex);

	return 0;
}


/**
@brief 掉电倒计时计数器重新计时
@param 无
@return
**/
int Ec600sTPwrOffReCountDown()
{
    MUTEX_LOCK(ec600sMux,MUTEX_WAIT_ALWAYS);
    g_ulTPwrOff=0;
    MUTEX_UNLOCK(ec600sMux);
    return 0;
}

/**
@brief 掉电延迟计数超时函数
@param ms 掉电延迟时间,单位毫秒
@return true超时,false未超时
**/
bool Ec600sPwrOffDelayTimeout(int ms)
{
    MUTEX_LOCK(ec600sMux,MUTEX_WAIT_ALWAYS);
    if(g_ulTPwrOff==0)
    {
        g_ulTPwrOff=OppTickCountGet();
        DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "***********recountdown\r\n");
    }
    if(OppTickCountGet()-g_ulTPwrOff>ms)
    {
        MUTEX_UNLOCK(ec600sMux);
        return true;
    }
    MUTEX_UNLOCK(ec600sMux);
    return false;
}

int Ec600sSleep(int ticks)
{
    DevEventProc(EN_DEV_TYPE_EC600S);
    Ec600sRxDataProc();
    vTaskDelay(ticks / portTICK_PERIOD_MS);

    return 0;
}


int Ec600sLoop(void)
{
    if(Ec600sLastRegStGet() != Ec600sRegStGet())
    {
        if(ec600s_dev.ops->state_chg)
            ec600s_dev.ops->state_chg();
        Ec600sLastRegStSet(Ec600sRegStGet());
    }

	if(Ec600sStGet() != Ec600sLastStGet())
    {
		if(ec600s_dev.ops->dev_chg)
			ec600s_dev.ops->dev_chg();
		Ec600sLastStSet(Ec600sStGet());
        Ec600sTPwrOffReCountDown();
	}

    if(Ec600sStGet()==EC600S_DEV_PWROFF)
    {
        DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "do pwron action\r\n");
        //EC600S关机后开机低电平需持续500ms,EC600N关机后开机低电平需持续650ms
        //EC600N新模组关机后开机低电平需持续放长到1s
        //EC800G开机后低电平需持续1650ms才会关机
        Ec600sPwrKeyPullDown(PWRKEYOFF_TIME);
        Ec600sStSet(EC600S_DEV_WAIT_PWRON);
    }
    /*else(Ec600sStGet()==WAIT_PWRON)
    {

    }*/
    else if(Ec600sStGet()==EC600S_DEV_PWRON)
    {
        if(0==Ec600sAT_FuncInit())
        {
            Ec600sStSet(EC600S_DEV_WAIT_READY);
        }
    }
    else if(Ec600sStGet()==EC600S_DEV_READY)
    {
        Ec600sTcpipLoop();
        TxDataProc();
    }
    else if(Ec600sStGet()==EC600S_DEV_WAIT_PWROFF)
    {
        //等待大于2s关机,留余量2秒.延迟4秒
        if(Ec600sPwrOffDelayTimeout(EC600S_PWROFF_DELAY))
        {
            Ec600sStSet(EC600S_DEV_PWROFF);
        }
    }
    else if(Ec600sStGet()==EC600S_DEV_RESET)
    {
        Ec600sRstLoop();
    }

    if(Ec600sStGet()>EC600S_DEV_WAIT_PWRON)
        Ec600sAtCmdQueueOut();

    Ec600sSleep(30);
    return 0;
}

/**
@brief ec600s设备状态超时处理函数
@return 0成功，非0失败
**/
int Ec600sDevStateTimeout()
{
	static U32 tick_start;
	static int init = 0;

	if(!init)
    {
		tick_start = OppTickCountGet();
		init = 1;
	}

	if(Ec600sLastStGet() != Ec600sStGet())
    {
		tick_start = 0;
	}
	else
    {
		if(Ec600sStGet() != EC600S_DEV_READY
			&& Ec600sStGet() != EC600S_DEV_TEST
			&& Ec600sStGet() != EC600S_DEV_DIS)
	    {
			if(tick_start == 0)
            {
				DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "devstate %d start count\r\n", Ec600sStGet());
				tick_start = OppTickCountGet();
			}
            else
            {
				if((OppTickCountGet() - tick_start) >= EC600S_DEVSTATE_DEFAULT_TO)
                {
					DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "devstate %d timeout\r\n", Ec600sStGet());
					Ec600sReset(EC600S_RST_CMD);
					tick_start = 0;
				}
			}
		}
	}
	return 0;
}


/**
@brief ec600s注册超时处理函数
@return 0成功，非0失败
**/
int Ec600sRegTimeout()
{
	static U32 tick_start = 0;

    //测试模式不重启
    if(Ec600sStGet()==EC600S_DEV_TEST)
    {
        tick_start = 0;
        return 0;
    }

	/*reg state process*/
	if(Ec600sLastRegStGet() != Ec600sRegStGet())
    {
		DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "Ec600sRegTimeout last %d cur %d\r\n",
			Ec600sLastRegStGet(), Ec600sRegStGet());
		if(REG == Ec600sRegStGet()
			|| ROAMING_REG == Ec600sRegStGet())
		{
			tick_start = 0;
		}
        else
        {
			tick_start = OppTickCountGet();
		}
	}
	if(tick_start > 0)
    {
		if(OppTickCountGet() - tick_start > EC600S_REGSTATE_DEFAULT_TO)
        {
			DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_INFO, "Ec600sRegTimeout\r\n");
            Ec600sReset(EC600S_RST_CMD);
			tick_start = 0;
		}
	}
	return 0;
}

/**
@brief ec600s状态变化处理函数
@return 0成功，非0失败
**/
int Ec600sStateChgReg(neul_state_chg func)
{
	ec600s_dev.ops->state_chg = func;

	return 0;
}

/**
@brief ec600s注册状态变化处理函数
@return 0成功，非0失败
**/
int Ec600sDevChgReg(neul_state_chg func)
{
	ec600s_dev.ops->dev_chg = func;

	return 0;
}

/**
@brief Ec600s主任务
@param 无
@return
**/
void Ec600sThread(void *pvParameter)
{
	//register callback function ???
	Ec600sStateChgReg(Ec600sRegStateChgCallback);
	Ec600sDevChgReg(Ec600sDevStateChgCallback);
	printf("-0tick:%d\r\n", OppTickCountGet());
	while(1){
        DALoop();
        Ec600sLoop();
        Ec600sSockLoop();
        Ec600sDevStateTimeout();
        Ec600sRegTimeout();
        Ec600sUsimLoop();
        Ec600sPrintInfoDelay(60000);
        WDG_Feed(*(int *)pvParameter);
        //Ec600sSaveDisLog(1800000);
        //taskWdtReset();
        //userWdtFeed(USER_WDT_TASK_3);
	}
}


